/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class LoginScene extends Scene {

    private static final int SCREEN_LOGIN = 0;
    private static final int SCREEN_REGISTER = 1;
    private static final int SCREEN_CHANGE = 2;
    //----------------------------------
    private boolean isRememberPassWord;
    //----------------------------------
    private int screenType;
    private int genderSelected;
    private int curseurIndex;
    private int numberCurseurIndex;
    //----------------------------------
    private String userNameShow;
    private String passwordClear;
    private String retypePasswordClear;
    private String userNameRead;
    private String passwordRead;
    //----------------------------------
    private CustomTextBox userNameBox;
    private CustomTextBox passWordBox;
    private CustomTextBox newPassWordBox;
    private CustomTextBox reTypePassWordBox;
    //----------------------------------
    private Image topBarImage;
    private Image chandelierImage;
    private Image logoImage;
    private Image bottomBarImage;
    private Image tickImage;
    private Image tickOkImage;
    private Recorder recorder;
    private String newPassClear;
    private boolean isActivated;
    private boolean isResetPass;
    private int keyCode;
    private String systaxActivate;
    private String numberActivate;
    private ReadDatFile readDatFile;

    public LoginScene(MainCanvas parent) {
        super(parent);
        initValue();
        initObject();
        readDataSave();
    }

    private void initValue() {
        numberCurseurIndex = 3;
        newPassClear = "";
        isActivated = true;
    }

    private void initObject() {
        try {
            initImage();
            initCustomBox();
        } catch (IOException ex) {
        }
    }

    public void setListSubMenu() {
        subMenu.setList(new String[]{"Đăng ký", "Gọi hỗ trợ", "Đổi mật khẩu", "Lấy lại MK", "Cập nhật bản mới", "Thoát"});
    }

    private void initCustomBox() {
        userNameBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
        passWordBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, true, keyPad);
        reTypePassWordBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, true, keyPad);
        newPassWordBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, true, keyPad);
        userNameBox.setFocus(true);
        passWordBox.setFocus(false);
        reTypePassWordBox.setFocus(false);
        newPassWordBox.setFocus(false);
    }

    private void initImage() throws IOException {
        chandelierImage = Utils.loadImage("den_chum.png");
        logoImage = Utils.loadImage("logo.png");
        bottomBarImage = library.getBottomBarImage();
        Image topBarPiece = Utils.loadImage("login_top_bar_piece.png");
        topBarImage = Utils.creatImageFromPiece(topBarPiece, 1, canvasWidth);
        tickImage = Utils.loadImage("tick.png");
        tickOkImage = Utils.loadImage("tick_ok.png");

    }

    private void readDataSave() {
        new Thread(new Runnable() {

            public void run() {
                recorder = new Recorder();
                recorder.openRecStore();
                userNameRead = recorder.getUserName();
                passwordRead = recorder.getPassWord();
//                userNameRead = "hoangk-0163-623-4612";
//                passwordRead = "ah0ag21dung";
                userNameShow = userNameRead;
                passwordClear = passwordRead;
                Debug.d("user name read :" + userNameShow);
                Debug.d("pass read :" + passwordClear);
                if (userNameShow == null) {
                    userNameShow = "";
                }

                if (passwordClear == null) {
                    passwordClear = "";
                } else {
                    if (passwordClear.length() >= 6) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i = 0; i < passwordClear.length(); i++) {
                            buffer.append("*");
                        }
                        passWordBox.setText(buffer.toString());
                        passWordBox.setPass(passwordClear);
                        userNameBox.setText(userNameShow);
                        parent.repaint();
                        isRememberPassWord = true;
                    }
                }
            }
        }).start();
    }
//------------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintBarMenu(g);
        paintLoginBox(g);

//        PiPoDesigner.drawString(g, String.valueOf(readDatFile.getId()), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, 20, 100);
//        PiPoDesigner.drawString(g, String.valueOf("key:" + keyCode), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, 20, 100);
    }

    protected void paintBarMenu(Graphics g) {
        int anchorX = canvasWidth / 2 - chandelierImage.getWidth() / 2;
        int anchorY = topScreen;
        g.drawImage(chandelierImage, anchorX, topScreen, 0);
        g.drawImage(chandelierImage, anchorX - chandelierImage.getWidth() / 2, topScreen, 0);
        g.drawImage(chandelierImage, anchorX + chandelierImage.getWidth() / 2, topScreen, 0);
        g.drawImage(topBarImage, leftScreen, topScreen, 0);
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
        PiPoDesigner.drawString(g, "Menu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
        if (!isShowSubMenu) {
            if (screenType == SCREEN_LOGIN) {
                PiPoDesigner.drawString(g, "Đăng nhập", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 30, canvasHeight - 25);
            } else if (screenType == SCREEN_REGISTER) {
                PiPoDesigner.drawString(g, "Đăng ký", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 25, canvasHeight - 25);
            } else if (screenType == SCREEN_CHANGE) {
                PiPoDesigner.drawString(g, "OK", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 15, canvasHeight - 25);
            }
            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 30, canvasHeight - 25);
        } else {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
        }
    }

    private void paintLoginBox(Graphics g) {
        if (screenMode == SCREEN_PORTRAIT) {
            g.drawImage(logoImage, canvasWidth / 2 - logoImage.getWidth() / 2, topScreen + 20, 0);
            // box user name
            PiPoDesigner.drawString(g, "Tên", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 40, topScreen + 140);
            userNameBox.paint(g, leftScreen + 70, topScreen + 135, canvasWidth - 80, 20);

            //box password
            PiPoDesigner.drawString(g, "M.Khẩu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + 180);
            passWordBox.paint(g, leftScreen + 70, topScreen + 175, canvasWidth - 80, 20);

            // remember pass box
            if (screenType == SCREEN_LOGIN) {
                if (isRememberPassWord) {
                    g.drawImage(tickOkImage, leftScreen + 75, topScreen + 215, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 75, topScreen + 215, 0);
                }
                PiPoDesigner.drawString(g, "Nhớ thông tin", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 75 + tickImage.getWidth() + 5, topScreen + 213);

            } else if (screenType == SCREEN_REGISTER) {
                //box re enter password
                PiPoDesigner.drawString(g, "Nhập lại", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                        leftScreen + 20, topScreen + 220);
                reTypePassWordBox.paint(g, leftScreen + 70, topScreen + 215, canvasWidth - 80, 20);

                if (genderSelected == 0) {
                    g.drawImage(tickOkImage, leftScreen + 75, topScreen + 258, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 75, topScreen + 258, 0);
                }
                PiPoDesigner.drawString(g, "Nam", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 75 + tickImage.getWidth() + 5, topScreen + 255);
                if (genderSelected == 1) {
                    g.drawImage(tickOkImage, leftScreen + 150, topScreen + 258, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 150, topScreen + 258, 0);
                }
                PiPoDesigner.drawString(g, "Nữ", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 150 + tickImage.getWidth() + 5, topScreen + 255);
            } else if (screenType == SCREEN_CHANGE) {
                //box re enter password
                PiPoDesigner.drawString(g, "MK mới", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                        leftScreen + 20, topScreen + 220);
                newPassWordBox.paint(g, leftScreen + 70, topScreen + 215, canvasWidth - 80, 20);
                PiPoDesigner.drawString(g, "Nhập lại", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                        leftScreen + 20, topScreen + 260);
                reTypePassWordBox.paint(g, leftScreen + 70, topScreen + 255, canvasWidth - 80, 20);
            }

            g.setColor(0xffffff);
            if (curseurIndex == 2 && screenType == SCREEN_LOGIN) {
                g.drawRoundRect(leftScreen + 70, topScreen + 208, tickImage.getWidth() + 100, 20, 5, 5);
            }
            if (curseurIndex == 3 && screenType == SCREEN_REGISTER) {
                g.drawRoundRect(leftScreen + 70, topScreen + 253, 120, 20, 5, 5);
            }
        } else {
//            g.drawImage(logoImage, leftScreen + 10, topScreen + 20, 0);
            //box user name 

            // remember pass box
            if (screenType == SCREEN_LOGIN) {
                g.drawImage(logoImage, leftScreen + 10, topScreen + 20, 0);
                int delta = 80;
                PiPoDesigner.drawString(g, "Tên", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 40, topScreen + delta);
                userNameBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                //box password
                delta += 40;
                PiPoDesigner.drawString(g, "M.Khẩu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + delta);
                passWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                delta += 40;
                if (isRememberPassWord) {
                    g.drawImage(tickOkImage, leftScreen + 75, topScreen + delta, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 75, topScreen + delta, 0);
                }
                PiPoDesigner.drawString(g, "Nhớ thông tin", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 75 + tickImage.getWidth() + 5, topScreen + delta - 3);
                g.setColor(0xffffff);
                if (curseurIndex == 2 && screenType == SCREEN_LOGIN) {
                    g.drawRoundRect(leftScreen + 70, topScreen + delta - 7, tickImage.getWidth() + 100, 20, 5, 5);
                }
            } else if (screenType == SCREEN_REGISTER) {
                int delta = 40;
                PiPoDesigner.drawString(g, "Tên", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 40, topScreen + delta);
                userNameBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                //box password
                delta += 40;
                PiPoDesigner.drawString(g, "M.Khẩu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + delta);
                passWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                delta += 40;
                //box re enter password
                PiPoDesigner.drawString(g, "Nhập lại", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                        leftScreen + 20, topScreen + delta);
                reTypePassWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                delta += 40;
                if (genderSelected == 0) {
                    g.drawImage(tickOkImage, leftScreen + 75, topScreen + delta, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 75, topScreen + delta, 0);
                }
                PiPoDesigner.drawString(g, "Nam", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 75 + tickImage.getWidth() + 5, topScreen + delta - 3);
                if (genderSelected == 1) {
                    g.drawImage(tickOkImage, leftScreen + 150, topScreen + delta, 0);
                } else {
                    g.drawImage(tickImage, leftScreen + 150, topScreen + delta, 0);
                }
                if (curseurIndex == 3) {
                    g.setColor(0xffffff);
                    g.drawRoundRect(leftScreen + 70, topScreen + delta - 7, 120, 20, 5, 5);
                }
                PiPoDesigner.drawString(g, "Nữ", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 150 + tickImage.getWidth() + 5, topScreen + delta - 3);
            } else if (screenType == SCREEN_CHANGE) {
                g.drawImage(logoImage, leftScreen + 10, topScreen + 20, 0);
                int delta = 80;
                PiPoDesigner.drawString(g, "Tên", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 40, topScreen + delta);
                userNameBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                //box password
                delta += 40;
                PiPoDesigner.drawString(g, "M.Khẩu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + delta);
                passWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                delta += 40;
                PiPoDesigner.drawString(g, "MK mới", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + delta);
                newPassWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
                delta += 40;
                PiPoDesigner.drawString(g, "Nhập lại", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + delta);
                reTypePassWordBox.paint(g, leftScreen + 70, topScreen + delta - 5, canvasWidth - 80, 20);
//                if (isRememberPassWord) {
//                    g.drawImage(tickOkImage, leftScreen + 75, topScreen + delta, 0);
//                } else {
//                    g.drawImage(tickImage, leftScreen + 75, topScreen + delta, 0);
//                }
//                PiPoDesigner.drawString(g, "Nhớ thông tin", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 75 + tickImage.getWidth() + 5, topScreen + delta - 3);
//                g.setColor(0xffffff);
//                if (curseurIndex == 2 && screenType == SCREEN_LOGIN) {
//                    g.drawRoundRect(leftScreen + 70, topScreen + delta - 7, tickImage.getWidth() + 100, 20, 5, 5);
//                }
            }
        }
    }

    //----------------------------------------------------------------------
    public void doLoop() {
        if (countFrame % 4 == 0) {
            doCustomBoxLoop();
            parent.repaint();
        }
    }

    private void doCustomBoxLoop() {
        userNameBox.run();
        passWordBox.run();
        reTypePassWordBox.run();
        newPassWordBox.run();
    }
//-----------------------------------------------------------------

    public void doKeyLeft() {
        if (curseurIndex == 3 && screenType == SCREEN_REGISTER) {
            genderSelected = 1 - genderSelected;
            parent.repaint();
            return;
        }
        userNameBox.moveCurseurLeft();
        passWordBox.moveCurseurLeft();
        reTypePassWordBox.moveCurseurLeft();
        parent.repaint();
    }

    public void doKeyRight() {
        if (curseurIndex == 3 && screenType == SCREEN_REGISTER) {
            genderSelected = 1 - genderSelected;
            parent.repaint();
        }
        userNameBox.moveCurseurRight();
        passWordBox.moveCurseurRight();
        reTypePassWordBox.moveCurseurRight();
        parent.repaint();
    }

    protected void doKeyUpRemain() {
        if (curseurIndex > 0) {
            curseurIndex--;
        }
        switchCurseur();
        parent.repaint();
    }

    protected void doKeyDownRemain() {
        curseurIndex = (++curseurIndex) % numberCurseurIndex;
        switchCurseur();
        parent.repaint();
    }

    protected void doSoftKeyLeftRemain() {
    }

    protected void doSoftKeyRightRemain() {
        userNameBox.doSoftKeyRight();
        passWordBox.doSoftKeyRight();
        reTypePassWordBox.doSoftKeyRight();
        newPassWordBox.doSoftKeyRight();
        parent.repaint();
    }

    protected void doKeyCenterRemain() {
        if (isLoading) {
            return;
        }
        if ((curseurIndex == 2) && (screenType == SCREEN_LOGIN)) {
            isRememberPassWord = !isRememberPassWord;
            parent.repaint();
            return;
        }
        if (screenType == SCREEN_LOGIN) {
            userNameShow = userNameBox.getText();
            passwordClear = passWordBox.getPassWord();
            Debug.d("user name :" + userNameShow);
            Debug.d("pass :" + passwordClear);
            if (userNameShow.length() < 6 || passwordClear.length() < 6) {
                dialogString = "Tên hoặc mật khẩu ít nhất 6 kí tự";
                parent.repaint();
                return;
            }
            isLoading = true;
            parent.repaint();
            connectServer();
            return;
        }
        if (screenType == SCREEN_REGISTER) {
            userNameShow = userNameBox.getText();
            passwordClear = passWordBox.getPassWord();
            retypePasswordClear = reTypePassWordBox.getPassWord();
            if (userNameShow.length() < 6 || passwordClear.length() < 6 || retypePasswordClear.length() < 6) {
                dialogString = "Tên hoặc mật khẩu ít nhất 6 kí tự";
                parent.repaint();
                return;
            }
            if (!passwordClear.equals(retypePasswordClear)) {
                dialogString = "Mật khẩu nhập lại không đúng";
                parent.repaint();
                return;
            }
            isLoading = true;
            parent.repaint();
            connectServer();
            return;
        }
        if (screenType == SCREEN_CHANGE) {
            userNameShow = userNameBox.getText();
            passwordClear = passWordBox.getPassWord();
            newPassClear = newPassWordBox.getPassWord();
            retypePasswordClear = reTypePassWordBox.getPassWord();
            if (userNameShow.length() < 6 || newPassClear.length() < 6 || retypePasswordClear.length() < 6) {
                dialogString = "Tên hoặc mật khẩu ít nhất 6 kí tự";
                parent.repaint();
                return;
            }
            if (!newPassClear.equals(retypePasswordClear)) {
                dialogString = "Mật khẩu nhập lại không đúng";
                parent.repaint();
                return;
            }
            isLoading = true;
            parent.repaint();
            connectServer();
            return;
        }
    }

    protected void doSubMenu() {
        switch (subMenu.getSelectedIndex()) {
            case 0:
                screenType = (screenType == 0) ? 1 : 0;
                if (screenType == SCREEN_LOGIN) {
                    subMenu.setList(0, "Đăng ký");
                    numberCurseurIndex = 3;
                } else {
                    subMenu.setList(0, "Đăng nhập");
                    numberCurseurIndex = 4;
                }
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 1:
                //goi ho tro
                parent.makeCall();
                break;
            case 2:
                numberCurseurIndex = 4;
                screenType = SCREEN_CHANGE;
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 3:
                userNameShow = userNameBox.getText();
                if (userNameShow.length() == 0) {
                    dialogString = "Bạn cần nhập tên tài khoản";
                    isShowSubMenu = false;
                    parent.repaint();
                    return;
                }

                isResetPass = true;
                connectServer();
                isLoading = true;
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 4:
                //update
                parent.openWeb(gameData.linkUpdate);
                isShowSubMenu = false;
                parent.repaint();
                break;
            case 5:
                parent.exit();
                break;
        }
    }

    public void inputKeyCode(int key) {
        this.keyCode = key;
        if (userNameBox.getText().length() < 20) {
            userNameBox.inputChar(key);
        }
        if (passWordBox.getPassWord().length() < 20) {
            passWordBox.inputChar(key);
        }
        if (reTypePassWordBox.getText().length() < 20) {
            reTypePassWordBox.inputChar(key);
        }
        if (newPassWordBox.getText().length() < 20) {
            newPassWordBox.inputChar(key);
        }
        parent.repaint();
    }
    //------------------------------------------------------------------------

    private void switchCurseur() {
        userNameBox.setFocus(false);
        passWordBox.setFocus(false);
        reTypePassWordBox.setFocus(false);
        newPassWordBox.setFocus(false);
        if (curseurIndex == 0) {
            userNameBox.setFocus(true);
            return;
        }
        if (curseurIndex == 1) {
            passWordBox.setFocus(true);
            return;
        }
        if (curseurIndex == 2 && screenType == SCREEN_REGISTER) {
            reTypePassWordBox.setFocus(true);
            return;
        }
        if (curseurIndex == 2 && screenType == SCREEN_CHANGE) {
            newPassWordBox.setFocus(true);
            return;
        }
        if (curseurIndex == 3 && screenType == SCREEN_CHANGE) {
            reTypePassWordBox.setFocus(true);
            return;
        }
    }

    private void connectServer() {
        Debug.d("connect to server");
        new Thread() {

            public void run() {
                initializeConnection();
            }
        }.start();

    }

    private void initializeConnection() {
        if (parent.initConnection()) {
            if (screenType == SCREEN_LOGIN) {
                if (!isResetPass) {
                    Request.login(userNameShow, passwordClear);
                } else {
                    Request.resetPass(userNameShow);
                }
            } else if (screenType == SCREEN_REGISTER) {
                readDatFile = new ReadDatFile();
                readDatFile.read("/info.dat");
                Request.register(userNameShow, passwordClear, genderSelected, readDatFile.getId());
            } else if (screenType == SCREEN_CHANGE) {
                Request.changePass(userNameShow, passwordClear, newPassClear);
            }
        } else {
            dialogString = "Không có kết nối đến máy chủ";
            isLoading = false;
            parent.repaint();
        }
    }

    protected void touchPressedRemain(int x, int y) {
        touchPressBox(x, y);
        touchRememberBox(x, y);
    }

    public void doTouchReleased(int x, int y) {
    }

    public void doTouchDraged(int x, int y) {
    }

    private void touchRememberBox(int x, int y) {
        if (isTouch(leftScreen + 50, topScreen + 205, 145, 50, x, y)) {
            if ((screenType == SCREEN_LOGIN)) {
                isRememberPassWord = !isRememberPassWord;
                parent.repaint();
                return;
            }
        }
    }

    private void touchPressBox(int x, int y) {
        userNameBox.setFocus(false);
        passWordBox.setFocus(false);
        reTypePassWordBox.setFocus(false);
        userNameBox.poiterPressed(x, y);
        passWordBox.poiterPressed(x, y);
        if (screenType == SCREEN_REGISTER) {
            reTypePassWordBox.poiterPressed(x, y);
        }
    }

    private void writeToRecord() {

        new Thread(new Runnable() {

            public void run() {
                try {
                    if (!isRememberPassWord) {
                        recorder.deleteRecStore();
                        return;
                    }
                    Debug.d("user name show :" + userNameShow);
                    Debug.d("user name read :" + userNameRead);
                    Debug.d("pass clear:" + passwordClear);
                    Debug.d("pass read:" + passwordRead);
                    if (userNameShow != null && (!userNameShow.equals(userNameRead) || !passwordClear.equals(passwordRead))) {
                        recorder.deleteRecStore();
                        recorder.writeRecord(userNameShow);
                        recorder.writeRecord(passwordClear);
                    }
                    userNameShow = null;
                    userNameRead = null;
                    passwordClear = null;
                    passwordRead = null;
                    recorder.closeRecStore();
                } catch (Exception ex) {
                }
            }
        }).start();
    }

    public void processMessage(String[] part) {
        super.processMessage(part);
        Debug.d("login process");
        if (part[0].equals("LO")) {
            if (Integer.parseInt(part[1]) == 1) {
                writeToRecord();
                gameData.setMyGender(Integer.parseInt(part[2]));
                gameData.setMyName(userNameShow);
                gameData.setMyMoney(Long.parseLong(part[3]));
                gameData.setAdString = part[10];
                gameData.linkUpdate = part[6];
                int currentVersion = Integer.parseInt(part[4]);
                Debug.d("current version :" + currentVersion);
                if (currentVersion > Constants.VERSION) {
                    questionString = "Bạn có muốn cập nhật phiên bản mới ?";
                    isLoading = false;
                    isNeedUpdate = true;
                    parent.repaint();
                    return;
                }
                if (isActivated) {
                    isLoading = false;
                    showChooseGame();
                    parent.repaint();
                    return;
                }

                return;

            }

            if (Integer.parseInt(part[1]) == 0) {
                isLoading = false;
                dialogString = "Tài khoản không tồn tại";
                parent.repaint();
                return;
            }

            if (Integer.parseInt(part[1]) == 2) {
                isLoading = false;
                dialogString = "Mật khẩu không đúng";
                parent.repaint();
                return;
            }
        }
        if (part[0].equals("ACTIVATE")) {
            isActivated = false;
            questionString = "Bạn có muốn kích hoạt tài khoản(500đ)?";
            isLoading = false;
            systaxActivate = part[1];
            numberActivate = part[2];
            parent.repaint();
            return;
        }

        if (part[0].equals("SMSINFO")) {

            SmsInfo smsInfo = new SmsInfo(part[4], part[3], part[1], part[7], part[5], part[11], part[9], part[2], part[6], part[10]);
            gameData.setSmsInfo(smsInfo);
            return;
        }

        if (part[0].equals("GRANTED")) {
            isLoading = false;
            isActivated = true;
            dialogString = "Bạn đã kích hoạt tài khoản thành công";
            parent.repaint();
        }
        if (part[0].equals("DENIED")) {
            isLoading = false;
            isActivated = true;
            dialogString = "Bạn đã dùng số điện thoại này để kích hoạt";
            parent.repaint();
        }

        if (part[0].equals("REGISTER")) {
            if (Integer.parseInt(part[1]) == 0) {
                dialogString = "Tài khoản này đã tồn tại";
                isLoading = false;
                parent.repaint();
                return;
            }
            if (Integer.parseInt(part[1]) == 1) {
                dialogString = "Bạn đã đăng ký thành công";
                screenType = SCREEN_LOGIN;
                subMenu.setList(0, "Đăng ký");
                numberCurseurIndex = 3;
                isLoading = false;
                parent.repaint();
                return;
            }
        }

        if (part[0].equals("CPASS")) {
            if (Integer.parseInt(part[1]) == 0) {
                dialogString = "Mật khẩu không đúng";
                isLoading = false;
                parent.repaint();
                return;
            } else {
                isLoading = false;
                dialogString = "Bạn đã đổi mật khẩu thành công";
                screenType = SCREEN_LOGIN;
                numberCurseurIndex = 3;
                passWordBox.setText(newPassClear);
                parent.repaint();
            }
            return;
        }

        if (part[0].equals("RSMK")) {
            int result = Integer.parseInt(part[1]);
            if (result == 0) {
                dialogString = "Tài khoản này không tồn tại";
            } else {
                numberActivate = part[2];
                systaxActivate = part[3];
                Debug.d("number active :" + numberActivate);
                questionString = "Bạn cần nhắn tin để lấy lại mật khẩu";
            }
            isLoading = false;
            parent.repaint();
            return;
        }
    }

    public void dispose() {

        retypePasswordClear = null;
        //----------------------------------
        userNameBox = null;
        passWordBox = null;
        reTypePassWordBox = null;
        //----------------------------------
        topBarImage = null;
        chandelierImage = null;
        logoImage = null;
        bottomBarImage = null;
        tickImage = null;
        tickOkImage = null;
        recorder = null;
        super.dispose();

    }

    protected void reponseQuestion(int result) {
        Debug.d("result :" + result);
        if (result == 1) {
            if (isResetPass) {
                isResetPass = false;
                Utils.sendSmsNap(numberActivate, systaxActivate + " MK " + userNameBox.getText());
                isLoading = true;
                parent.repaint();
                Thread thread = new Thread(new Runnable() {

                    public void run() {
                        int sendResult = -1;
                        while (sendResult == -1) {
                            sendResult = Utils.isSent();
                            Debug.d("result send :" + sendResult);
                        }
                        if (sendResult == 1) {
                            isLoading = false;
                            dialogString = "Tin nhắn MK sẽ được gửi cho bạn.";
                            parent.repaint();
                        }
                    }
                });
                thread.start();
                return;
            }
            if (isNeedUpdate) {
                Debug.d("yes");
                parent.openWeb(gameData.linkUpdate);
                parent.repaint();
                return;
            }

            Utils.sendSmsNap(numberActivate, systaxActivate + " " + userNameBox.getText());
            isLoading = true;
            parent.repaint();
            Thread thread = new Thread(new Runnable() {

                public void run() {
                    int sendResult = -1;
                    while (sendResult == -1) {
                        sendResult = Utils.isSent();
                        Debug.d("result send :" + sendResult);
                    }
                    if (sendResult == 1) {
                        isLoading = true;
                        parent.repaint();
                    }
                }
            });
            thread.start();
        } else {
            if (isNeedUpdate) {
                Debug.d("no");
                isLoading = true;
                showChooseGame();
                parent.repaint();
                return;
            }
            Request.quit();
        }
    }

    public void doKeyPound() {
        userNameBox.doKeyPound();
        passWordBox.doKeyPound();
        reTypePassWordBox.doKeyPound();
        newPassWordBox.doKeyPound();
    }

    void onSizeChange(int w, int h) {
        userNameBox.sizeChage(w, h);
        passWordBox.sizeChage(w, h);
        newPassWordBox.sizeChage(w, h);
        reTypePassWordBox.sizeChage(w, h);
    }
}
